<script setup>
import {
    Edit, Delete, Position, Upload
} from '@element-plus/icons-vue'

import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
    addstaffservice, getpartpathservice, getalljobsservice, getpartstaffservice,
    updatepathservice, deletepathservice, deletestaffservice, updatestaffservice,
} from '@/api/book.js'

//控制添加分类弹窗
const dialogVisible = ref(false)
//分页条数据模型
//const pageNum = ref(1)//当前页
const total = ref(20)//总条数
//const pageSize = ref(3)//每页条数
//文章分类数据模型

//定义变量，控制标题
const title = ref('')

//交通路线列表数据模型
const majors = ref([])

const alljobs = ref([])
//与后端交互的json数据
const pathmodel = ref({
    id: null,
    name: null,
    introduction: null,
})

const findmodel = ref({
    pageNum: 1,
    pageSize: 5,
    str: null,

})
const stafflist = async () => {
    console.log(findmodel.value);
    let result = await getpartstaffservice(findmodel.value);
    console.log(result);
    total.value = result.data.total;
    majors.value = result.data.items;
}
stafflist();
//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    findmodel.value.pageSize = size;
    stafflist();
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    findmodel.value.pageNum = num;
    stafflist();
}
const cleardata = () => {
    findmodel.value.str = null
}
//表单校验
const rules = {
    name: [
        { required: true, message: '请输入专业名称', trigger: 'blur' },
    ],
    introduction: [
        { required: true, message: '请输入专业简介', trigger: 'blur' },
    ],

}
const add = () => {
    pathmodel.value.name = null
    pathmodel.value.introduction = null
    dialogVisible.value = true;
    title.value = '添加专业'
}
const addstaff = async () => {
    console.log(pathmodel.value);
    let result = await addstaffservice(pathmodel.value);
    console.log(result);
    if (result.code == 0) {
        ElMessage.success("添加成功")
        dialogVisible.value = false;
        stafflist();
    }
    else {
        ElMessage.error(result.message ? result.message : '添加失败')
    }
}
const updatestaff = async () => {
    console.log(pathmodel.value);
    let result = await updatestaffservice(pathmodel.value);
    if (result.code == 0) {
        ElMessage.success("修改成功")
        dialogVisible.value = false;
        stafflist();
    }
    else {
        ElMessage.error(result.message ? result.message : '修改失败')
    }
}
//删除分类
const del = (row) => {
    ElMessageBox.confirm(
        '你确认删除该条专业信息吗？',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            //用户点击了确认
            let result = await deletestaffservice(row.id)
            if (result.code != 0) ElMessage.error(result.message ? result.message : '删除失败')
            else ElMessage.success('删除成功')
            //再次调用getAllCategory，获取所有文章分类
            stafflist();
        })
        .catch(() => {
            //用户点击了取消
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })
}
//展示编辑弹窗
const showDialog = (row) => {
    title.value = '编辑专业信息';
    dialogVisible.value = true;
    //数据拷贝

    pathmodel.value.name = row.name
    pathmodel.value.introduction = row.introduction

    //扩展i的属性，将来需要传递给后台，完成分类的修改
    pathmodel.value.id = row.id;
    console.log(row.id);
}
const moreName = ref('更多查询条件')
const moreselect = ref(false)
const more = () => {
    if (moreName.value == '更多查询条件') {
        moreselect.value = true
        moreName.value = '返回'
    }
    else {
        moreselect.value = false
        moreName.value = '更多查询条件'
    }
}
</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>专业信息管理</span>
                <div class="extra">
                    <el-button type="primary" @click="add">添加专业</el-button>
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->
        <el-form>
            <el-form-item label="查询条件:">
                <el-input v-model="findmodel.str" placeholder="请输入查询内容" style="width: 300px"></el-input>
                <el-button type="primary" @click="stafflist" style="margin-left: 15px;">搜索</el-button>
                <el-button @click="cleardata">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 文章列表 -->
        <el-table :data="majors" style="width: 100%">
            <el-table-column label="专业编号" prop="id"></el-table-column>
            <el-table-column label="专业名称" prop="name"></el-table-column>
            <el-table-column label="专业简介" prop="introduction"></el-table-column>
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="del(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页条 -->
        <el-pagination v-model:current-page="findmodel.pageNum" v-model:page-size="findmodel.pageSize"
            :page-sizes="[5, 10, 15, 20]" layout="jumper, total, sizes, prev, pager, next" background :total="total"
            @size-change="onSizeChange" @current-change="onCurrentChange"
            style="margin-top: 20px; justify-content: flex-end" />
    </el-card>
    <!-- 添加分类弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="25%">
        <el-form :model="pathmodel" :rules="rules" label-width="90px" style="padding-right: 30px">
            <el-form-item label="专业名称" prop="name">
                <el-input v-model="pathmodel.name" placeholder="请输入专业名称"></el-input>
            </el-form-item>
            <el-form-item label="专业简介" prop="introduction">
                <el-input v-model="pathmodel.introduction" type="textarea" :rows="3" placeholder="请输入专业简介">
                </el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="title == '添加专业' ? addstaff() : updatestaff()"> 确认 </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>